var currentPage;

function setUp(){
	var qs = window.location.search;
	var id = qs.match(/\?.*?p=(\w+)/);
	currentPage = id[1];
	$('#filter_' + id[1]).attr('checked', true);
	
	$('#filterOptions').buttonset();
	$('#areaOptions').buttonset();
	
	$('input[name=areaFilter]').bind('click', function(){
		window.location = "shares?p=" + $(this).val();
	});

	

	$('#PBDeleteMessages').bind('click', function(){
		deleteMessages();
	});
}

function deleteMessages(){
	toDelete = $('input[type=checkbox]:checked').parents('label');
	
	if(toDelete.length > 0){
		if(currentPage == "inbox"){
			var confirmed = confirm(PicBoard.languageMap.lang_210_deleteInbox);
		} else {
			var confirmed = confirm(PicBoard.languageMap.lang_207_deleteMessages);		
		}
		var idsToDelete = { ids:[], type: currentPage };
		var objectsToDelete = [];

		if(confirmed){
			toDelete.each(function(i){
				// Get the ID of the chart.
				currHref = $(this).find('a').attr('href');
				currHref = currHref.match(/=(\d+)$/);
				
				id = currHref[1];			
				
				ouID = $(this).find('input[type=hidden]').val();
				
				objectsToDelete.push( $(this) );
				idsToDelete.ids.push( {'chID': id, 'ouID': ouID} );
			});
			
			//console.dir(idsToDelete);
			
			$.post(PicBoard.baseURL + 'rsrc.php?req=deleteShares', $.param(idsToDelete), function(data){
				//console.dir(data);
				if(data.statusCode == 200){
					$.each(objectsToDelete, function(i, v){
						$(v).remove();
					});
				} else {
					jQuery.facebox(data.message);
				}
			}, 'json' );
			
		}
	
	}
}

function initialise(){
	PicBoard.getTranslations(['lang_207_deleteMessages', 'lang_210_deleteInbox'], function(data){
		setUp();
	})
}

PicBoard.addLoadEvent( "initialise()" );